package listdomain;

## This file is part of the Simplemail project.
#  Copyright (c) 2008 the Simplemail Team (see authors).
# 
#  For the full copyright and license information, please view the COPYING
#  file that was distributed with this source code. You can also view the
#  COPYING file online at http://unixforum.org.ua/misc/COPYING.txt
# 
#  @copyright  (c) the authors
#  @author     Dmitriy Shilenko <rtyug@ukr.net>
#  @license    GPLv2 http://unixforum.org.ua/misc/COPYING.txt
#  @package    Panel Mailbox
#  @version    $Id: $

use strict;
use CGI::Carp qw(fatalsToBrowser);
use base 'func';

sub new {
    my $self = shift->SUPER::new(@_);
    $self->dbi_connect if $self->can("dbi_connect");
    return $self;
}

sub total_list_domain {
    my $self = shift;

    if ( $self->{p}->{username_domain} ) {
        $self->{p}->{username} = $self->{p}->{username_domain};
    }

    if ( $self->{p}->{username} ) {
        $self->{p}->{username} = $self->{p}->{username};
    }

    my $cl;
    my @cl0 = ();
    if ( defined( $self->{p}->{username} )
        && $self->{session}->{name} eq 'Admin' )
    {

        $self->{p}->{url} = '&username=' . $self->{p}->{username};

        $cl = "domain AS t1,
        users AS t2
       WHERE t1.idnane = t2.id AND t2.name = ?";
        push( @cl0, $self->{p}->{username} );
    }
    else {
        $cl = 'domain WHERE idnane = ?';
        push( @cl0, $self->{session}->{user} );
    }

    my $sth = $self->{dbh}->prepare("SELECT count(*) FROM $cl");
    $sth->execute(@cl0);

    $self->{p}->{total_number} = $sth->fetchrow_array();
    $sth->finish();

    return $self;
}

sub page_list_domain {
    my $self = shift;

    $self->{p}->{file} = 'listdomain';
    $self->SUPER::page_list(@_);
    return $self;
}

sub return_list_domain {
    my $self = shift;

    my $sth = $self->{dbh}->prepare(
        'SELECT name
                         FROM users 
                         ORDER BY created
                             '
    );
    $sth->execute();

    my @admin = ();
    while ( my $href = $sth->fetchrow_hashref() ) {
        push( @admin, $href->{name} );
    }
    $sth->finish();

##

    if ( defined( $self->{p}->{username} ) ) {
        my ( $index, $index2, $found );

        for ( $index = 0 ; $index < @admin ; $index++ ) {
            if ( $admin[$index] eq $self->{p}->{username} ) {
                $index2 = $index;
                $found  = 1;
                last;
            }
        }

        if ($found) {
            splice @admin, $index2, 1;
            my $i = $#admin + 1;
            $admin[$i] = $self->{p}->{username};
        }
    }
    my @admin2 = map +{ admin => $_, admin2 => $_ }, @admin;

    my @ds = ();
    my @cl = ();
    if ( $self->{session}->{name} eq 'Admin'
        && defined( $self->{p}->{username} ) )
    {
        push @ds, $self->{p}->{username};
        push @cl, "AND t3.name = ?";
    }
    else {
        push @ds, $self->{session}->{user};
        push @cl, "AND t3.id = ?";
    }

    my $sth = $self->{dbh}->prepare(
        "SELECT t1.idnane,
                                t1.aliases,
                                t1.domain,
                                t1.mailboxes,
                                t1.description,
                                t1.maxquota,
                                t1.backupmx,
                                t1.modified,
                                t1.active,
                               (select count(*) from mailbox where domain = t1.domain) AS countmail,
                               (select count(*) from alias where domain = t1.domain) AS countalias
                         FROM domain AS t1,
                              users AS t3
                        LEFT JOIN admin_domain AS t2
                        ON t3.id = t2.idname                         
                         WHERE
                         t1.idnane = t3.id @cl
                         ORDER BY id desc
                         LIMIT ?,?
                          "
    );
    $sth->execute( @ds, $self->{p}->{jj}, $self->{se}->{line_from_page} );

    my $loop_data;

    push @{$loop_data}, $_ while $_ = $sth->fetchrow_hashref();
    $sth->finish();

    foreach $_ ( @{$loop_data} ) {
        $_->{active} = $_->{active} == 1 ? 'YES' : 'NO';
        $_->{description} ||= 'No Description';
        $_->{from} = $self->{p}->{from} if ( $self->{p}->{from} );
        $_->{username2} = $self->{p}->{username}
          if ( $self->{p}->{username} && $self->{session}->{name} eq 'Admin' );
        if ( $_->{maxquota} ne '-1' && $_->{maxquota} ne '0' ) {
            $_->{maxquota} = sprintf( '%.1f', $_->{maxquota} / 1048576 );

        }
    }

    if ( !$loop_data ) {
        $self->{he}->{NOTS} = 1;
    }
    else {
        $self->{he}->{ROWS} = $loop_data;
    }

    if ( defined( $self->{p}->{username} )
        && $self->{session}->{name} eq 'Admin' )
    {
        $self->{he}->{ADMIN_S} = $self->{p}->{username};
    }
    else {
        $self->{he}->{ADMIN_S} = $self->{session}->{name};
    }

    $self->{he}->{ADMIN_SE} = 1 if ( $self->{session}->{name} eq 'Admin' );
    $self->{he}->{ADMIN} = \@admin2 if ( $self->{session}->{name} eq 'Admin' );

    return $self;
}

sub templ_hash_domain_list {
    my $self = shift;

    $self->{session}->{filet} = 'listdomain';

    $self->SUPER::templ_hash(@_);
}

sub DESTROY {
    my $self = shift;
    $self->SUPER::DESTROY if $self->can("SUPER::DESTROY");
}

1;
